﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.BIW;
using yunbo.Model.SM;

namespace yunbo.Repositorys.SMRepositorys
{
    public class WMS_RKRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";
        public int PostWMS_RK(TJWMS_RK tJWMS_RK)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"insert into wms_rk(rk_code,rk_type,ck_code,rk_fzr,rk_jhrksj,rk_state,create_by,create_time)\r\n" +
                    $"values('{tJWMS_RK.rk_code}','{tJWMS_RK.rk_type}','{tJWMS_RK.ck_code}',{tJWMS_RK.rk_fzr},'{tJWMS_RK.rk_jhrksj}',{tJWMS_RK.rk_state},{tJWMS_RK.create_by},'{tJWMS_RK.create_time}')";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
        public FYCXWMS_RK GetCXWMS_RK(int PageNumber, int PageSize)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"select e.ckb_code,a.rk_id,c.ck_name,d.user_uname,a.rk_code,a.ck_code,a.rk_state,a.rk_jhrksj,b.wldb_code,b.wldb_dblx,a.rk_type from wms_rk a\r\n" +
                    $"inner join [dbo].[wms_wldb] b on a.rk_code=b.rk_code\r\n" +
                    $"inner join [dbo].[wbd_ck] c on a.ck_code=c.ck_code\r\n" +
                    $"inner join [dbo].[user] d on a.rk_fzr=d.user_id\r\n" +
                    $"inner join [dbo].[wms_ck] e on e.ckb_code=b.ckb_code\r\n" +
                    $"where a.rk_type='调拨入库' \r\n" +
                    $"ORDER BY  [rk_id] \r\n" +
                    $"OFFSET ({PageNumber}-1)*{PageSize} ROWS\r\n" +
                    $"FETCH NEXT {PageSize} ROWS ONLY;"; ;
                List<CXWMS_RK> cXWMS_RKs = connection.Query<CXWMS_RK>(sql).ToList();
                string sql1 = $"select COUNT(*) from wms_rk a\r\n" +
                    $"inner join [dbo].[wms_wldb] b on a.rk_code=b.rk_code\r\n" +
                    $"inner join [dbo].[wbd_ck] c on a.ck_code=c.ck_code\r\n" +
                    $"inner join [dbo].[user] d on a.rk_fzr=d.user_id\r\n" +
                    $"inner join [dbo].[wms_ck] e on e.ckb_code=b.ckb_code\r\n" +
                    $"where a.rk_type='调拨入库'";
                int zon = connection.Query<int>(sql1).FirstOrDefault();
                FYCXWMS_RK fYCXWMS_RK = new FYCXWMS_RK()
                {
                    cXWMS_RKs = cXWMS_RKs,
                    zon = zon,
                };
                return fYCXWMS_RK;
            }
        }
        public int UpdatWMS_RK(int fzr,int uid,string code,int zt)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update wms_rk set rk_state={zt},rk_fzr={fzr},rk_sj='{DateTime.Now}',update_by={uid},update_time='{DateTime.Now}' where rk_code='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
    }
}
